package com.hongkzh.www.init.controller;

import com.hongkzh.www.adv.service.MediaService;
import com.hongkzh.www.conmon.RedisTool;
import com.hongkzh.www.conmon.ReqResponse;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author martins
 */
@Api(tag = "init", description = "初始化数据")
public class InitController extends Controller {
    MediaService mediaService=new MediaService();
    @ApiOperation(url = "/v1/init/data", tag = "init", httpMethod = "post",description = "初始化数据")
    @Params({

            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "version", description = "版本", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ " +
                            "recruitIndustrys 行业[id  parentId 父id  name 名称 ] " +
                            "recruitCategorys 职位类别[id  parentId 父id  name 名称 ] " +
                            " dict所有下拉数据[label显示名称  id  type 类型 description 描述 sort 排序]" +
                            "allCategorys 所有分类 [imgSrc 图片地址 ,categoryId 分类id type 1 自媒体 2 企业 3城市  name 名称]  }" +
                            "allMediaCategorys 所有自媒体分类 [imgSrc 图片地址 ,categoryId 分类id type 1 自媒体 2 企业 3城市  name 名称]  }" +
                            "area 所有国家 [id  parentId 父id  name 名称 type 0州 1国家 2省 3市 4区县 sort 排序]  }"
                    ),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
//    @Clear
    public void data(){
        ReqResponse<Record> r=new ReqResponse();
        Record record=new Record();
        List<Record> list=new ArrayList<>();
        Set<byte[]> set=RedisTool.getSmembers("dict");
        for (byte[] str : set) {
            list.add((Record) RedisTool.valueFromBytes(str));
         }
        record.set("dict",list);

        List<Record> recruitCategorys=new ArrayList<>();
        Set<byte[]> recruitCategorysset=RedisTool.getSmembers("recruitCategorys");
        for (byte[] str : recruitCategorysset) {
            recruitCategorys.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("recruitCategorys",recruitCategorys);

        List<Record> recruitIndustrys=new ArrayList<>();
        Set<byte[]> recruitIndustrysset=RedisTool.getSmembers("recruitIndustrys");
        for (byte[] str : recruitIndustrysset) {
            recruitIndustrys.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("recruitIndustrys",recruitIndustrys);
      List<Record> allCategorys=new ArrayList<>();
        Set<byte[]> allCategorysset=RedisTool.getSmembers("allCategorys");
        for (byte[] str : allCategorysset) {
            allCategorys.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("allCategorys",allCategorys);

        List<Record> area=new ArrayList<>();
        Set<byte[]> areaset=RedisTool.getSmembers("area");
        for (byte[] str : areaset) {
            area.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("area",area);
         record.set("allMediaCategorys",mediaService.AllCategoryList());
        r.setData(record);

        renderJson(r);
    }
    @ApiOperation(url = "/v1/init/shopData", tag = "init", httpMethod = "post",description = "初始化电商数据")
    @Params({

            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "version", description = "版本", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ " +
                            "系统电商分类 systemShopCategorys [categoryId 分类id parentId 父id  name 名称 ] " +

                            "allMediaShopCategorys 自媒体电商分类 [   mediaCategoryId 分类id  parentId 父id    name 名称]  }" +
                            "mediaAreas 自媒体快递地区 [id  parentId 父id  name 名称  ]  }"
                    ),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Clear
    public void shopData() {
        ReqResponse<Record> r = new ReqResponse();
        Record record = new Record();

        List<Record> systemShopCategorys=new ArrayList<>();
        Set<byte[]> systemShopCategorysset=RedisTool.getSmembers("systemShopCategorys");
        for (byte[] str : systemShopCategorysset) {
            systemShopCategorys.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("systemShopCategorys",systemShopCategorys);

        List<Record> allMediaShopCategorys=new ArrayList<>();
        Set<byte[]> allMediaShopCategorysset=RedisTool.getSmembers("allMediaShopCategorys");
        for (byte[] str : allMediaShopCategorysset) {
            allMediaShopCategorys.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("allMediaShopCategorys",allMediaShopCategorys);


        List<Record> mediaAreas=new ArrayList<>();
        Set<byte[]> mediaAreasset=RedisTool.getSmembers("mediaAreas");
        for (byte[] str : mediaAreasset) {
            mediaAreas.add((Record) RedisTool.valueFromBytes(str));
        }
        record.set("mediaAreas",mediaAreas);


        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/init/updateVersion", tag = "init", httpMethod = "post",description = "app检测版本")
    @Params({

            @Param(name = "version", description = "版本", required = true, dataType = "string"),
            @Param(name = "isIos", description = "是否isIos 0-ios 1-android", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ version 版本 note 说明 url 下载路径 createDate 更新时间 isOpen 0-强制升级 1-提示升级 2-不升级 isIos 0-ios 1-android}"  ),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Clear
    public void updateVersion() {
        String v1=getPara("version").replaceAll("\\.","");
        int version=Integer.parseInt(v1);
        String isIos = getPara("isIos");
       Record record= mediaService.updateVersion(version,isIos);
        ReqResponse<Record> r = new ReqResponse();
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/init/iosConfig", tag = "init", httpMethod = "post",description = "ios配置")

    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ isOpen 0-审核中 1-审核通过 2-强制升级 }"  ),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Clear
    public void iosConfig() {

        Record record= mediaService.iosConfig();
        ReqResponse<Record> r = new ReqResponse();
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/init/iosConfigEx", tag = "init", httpMethod = "post",description = "ios配置")
    @Params({

            @Param(name = "version", description = "版本", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ isOpen 0-审核中 1-审核通过 2-强制升级 }"  ),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Clear
    public void iosConfigEx() {
        String version=getPara("version");
        Record record= mediaService.iosConfigByiosVersion(version);
        ReqResponse<Record> r = new ReqResponse();
        r.setData(record);
        renderJson(r);
    }
}
